package jsr166.contrib.uncontended;

import java.lang.ref.WeakReference;

/**
 * This lock can only be obtained in the thread that created the lock.
 * If any other thread tries to obtain the lock, a UnexpectedConcurrentAccessException
 * is thrown;
 *
 */
public class ThreadConfinedLock extends AbstractUncontendedLock {

    final WeakReference<Thread> owner;
    
    public ThreadConfinedLock() {
        this(Thread.currentThread());
    }
    
    public ThreadConfinedLock(Thread owner) {
        this.owner =  new WeakReference<Thread>(owner);
    }
    
    
    /**
     * @throws UnexpectedConcurrentAccessException if called in any thread other than the one
     * that created the lock
     */
    public void lock() {
        if (Thread.currentThread() != owner.get())
            throw new UnexpectedConcurrentAccessException("Can only be accessed in thread that created the lock ", owner.get());
    }

    public void unlock() {

    }

}
